#!/usr/bin/env python3
# vim:fileencoding=utf-8

'''
文本方式查看 Empathy 的聊天记录

2010年9月25日
'''

import sys, os
from lxml import etree
import datetime

def convertMessage(msgs, file=sys.stdout):
  firstOfDay = None
  for msg in msgs:
    date = datetime.datetime.strptime(msg.get("time"), '%Y%m%dT%H:%M:%S')
    thisday = datetime.date(date.year, date.month, date.day)
    showdate = True
    if firstOfDay is None:
      firstOfDay = thisday
    elif firstOfDay == thisday:
      showdate = False
    else:
      firstOfDay = thisday
    text = msg.text
    name = msg.get('name')

    if showdate:
      print('==== %s ====' % firstOfDay.strftime('%Y年%m月%d日'), file=file)
    print('%-15s %s' % (name, date.strftime('%H:%M:%S')), text,
        file=file, sep='\n', end='\n\n')

def procFiles(files):
  msgs = []
  for f in files:
    doc = etree.parse(open(f))
    msgs.extend(doc.findall('message'))
  convertMessage(msgs)

if __name__ == '__main__':
  files = [i for i in sys.argv[1:]]
  procFiles(files)

